package cn.jimmie.learn.viewArt.weight

import android.animation.Animator
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.animation.LinearInterpolator

/**
 * function :
 *
 * <p> Create Time: 2019年01月14日 20:35 </p>
 * <p> @author Jimmie.Qian  </p>
 */
class HeartView(context: Context, attrs: AttributeSet) : BaseView(context, attrs),
    ValueAnimator.AnimatorUpdateListener, Animator.AnimatorListener {

    var text = ""

    private val animator = ValueAnimator.ofInt(3_000)
    private val path = Path()
    private var heartDrawing = false
    private var heart = 0.1f
    private var pct = 0f

    init {
        paint.strokeWidth = 4 * dp
        paint.textSize = 30 * dp
        paint.textAlign = Paint.Align.CENTER

        animator.interpolator = LinearInterpolator()
        animator.addUpdateListener(this)
        animator.addListener(this)
    }

    fun startHeart() {
        heartDrawing = true
        animator.cancel()
        animator.duration = 10_000
        animator.start()
    }

    override fun onDraw(canvas: Canvas) {
        path.reset()
        paint.color = Color.RED
        paint.style = Paint.Style.STROKE

        val padding = 20
        val halfWidth = width / 2 - padding
        val halfHeight = height / 2
        for (index in 0..(width - padding * 2)) {
            val x = (index - halfWidth) * 1.81 / halfWidth
            val y = -getHeartY(x, heart) * height / 6 + halfHeight
            if (index == 0) {
                path.moveTo(index.toFloat() + padding, y.toFloat())
            } else {
                path.lineTo(index.toFloat() + padding, y.toFloat())
            }
        }
        canvas.drawPath(path, paint)

        if (!heartDrawing) {
            println("drawTExt? ${pct}")
            val textWidth = paint.measureText(text)
            val left = width / 2f - textWidth / 2f
            val top = height * 0.4f - 0.9f * paint.textSize

            canvas.clipRect(left, top, left + textWidth * pct, top + 1.2f * paint.textSize)

            paint.color = Color.WHITE
            paint.style = Paint.Style.FILL
            canvas.drawText(text, width / 2f, height * 0.4f, paint)
        }
    }

    private fun getHeartY(x: Double, a: Float): Double {
        return Math.pow(x * x, 1.0 / 3) + 0.9 * Math.sqrt(3.3 - x * x) * Math.sin(a * Math.PI * x)
    }


    override fun onAnimationUpdate(animation: ValueAnimator) {
        val value = animation.animatedValue as Int
        if (value == 0) return
        if (heartDrawing) heart = value * 0.01f
        else {
            pct = value / 3000f
        }
        println("pct:$pct,heartDrawing:${heartDrawing}")
        invalidate()
    }

    override fun onDetachedFromWindow() {
        animator.cancel()
        animator.removeAllUpdateListeners()
        animator.removeAllListeners()
        super.onDetachedFromWindow()
    }

    override fun onAnimationEnd(animation: Animator?) {
        if (heartDrawing) {
            heartDrawing = false
            animator.duration = 3_000
            animator.start()
            println("开始?")
        }
    }

    override fun onAnimationStart(animation: Animator?) {
    }

    override fun onAnimationRepeat(animation: Animator?) {
    }

    override fun onAnimationCancel(animation: Animator?) {
    }
}